//
//  InvestmentViewModel.m
//  CBT
//
//  Created by Mr.Zhang on 2018/12/4.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "InvestmentViewModel.h"

@implementation InvestmentViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp{
    
    _interestSubject = [RACSubject new];
    
    _investmentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {

        if ([NSString isBlankString:input[@"num"]]) {
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入挖矿数量",nil)];
            return [RACSignal error:nil];
        }
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [UserManager userDoInvestWithUserId:input[@"userID"] coinId:input[@"coinId"] value:input[@"num"] day:input[@"day"] block:^(id data, HttpRequestResult result, NSString *msg) {
               
                if (result == kHttpRequestResultNormal) {
                    [HFStatusView showSuccessMessage:kLocalizedString(@"挖矿成功",nil)];
                    [subscriber sendNext:data];
                    [subscriber sendCompleted];
                }else{
                    [HFStatusView showFailureMessage:msg];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                }
                
            }];
            
            return [RACDisposable disposableWithBlock:^{
                NSLog(@"挖矿页面操作结束了");
            }];
        }];
    }];
}

-(void)getInvestRatioWithCoinId:(NSString *)coinId
{
    @weak(self)
    [UserManager getInvestRatioWithCoinId:coinId block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            InvestRatioModel *model = [[InvestRatioModel alloc] init];
            NSDictionary *dict = data[@"data"];
            model.ratio   = dict[@"ratio"];
            
            model.message = data[@"message"];
            model.code    = data[@"code"];

//            for (int i = 0; i < model.data.count; i++) {
//                InvestRatioItemModel *itemModel = model.data[i];
//                if (i == model.data.count - 1) {
//                    itemModel.isCellSelect = YES;
//                }else{
//                    itemModel.isCellSelect = NO;
//                }
//            }
            
            [self_weak_.interestSubject sendNext:model];
        }else{
            [self_weak_.interestSubject sendError:nil];
        }
    }];
}

@end
